package com.example.doubao_ai_demo.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.doubao_ai_demo.utils.HttpClientUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class WxLoginService {
    @Value("${wechat.appid}")
    private String appid;
    @Value("${wechat.secret}")
    private String secret;
    @Value("${wechat.wz}")
    private String wz_url;


    public String wxLogin(String code) {
        HashMap<String, String> map = new HashMap<>();
        map.put("appid", appid);
        map.put("secret", secret);
        map.put("js_code", code);
        map.put("grant_type", "authorization_code");
        JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doGet(wz_url, map));
        //查看用户是否存在
        //User byId = userMapper.selectById(jsonObject.getString("openid"));
        String openid = jsonObject.getString("openid");
        return "token" + openid;
    }
}
